package com.three.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smartcar.common.domain.request.DriverRequest;
import com.smartcar.common.domain.request.OrderRequest;
import com.smartcar.common.domain.request.OrderStatePageVo;
import com.smartcar.common.result.Result;
import com.three.common.request.FenceRequest;
import com.three.common.response.SendOrderResponse;
import com.three.server.service.OrderService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName : driverMe_dev
 * @PackageName : com.three.server.controller
 * @ClassName : OrderController
 * @Author : ZhouTeng
 * @CreateTime : 2023/10/15 16:14
 */

@RestController
@Log4j2
public class OrderController {

    @Autowired
    private OrderService orderService;

    // TODO 用户添加订单 -用户调用接口
    @PostMapping("/insertOrder")
    public Result insertUserOrder(@RequestBody OrderRequest orderRequest) {
        log.info("方法名称：用户添加订单，入参:{}", JSON.toJSONString(orderRequest));
        int i = orderService.insertOrder(orderRequest);
        Result result = null;
        if (i > 0) {
            result = Result.success(i);
            log.info("方法名称：用户添加订单，出参：{}", JSONObject.toJSONString(result));
        } else if(-1001 == i) {
            result = Result.error("起点不在服务区");
            log.info("方法名称：用户添加订单，出参：{}", JSONObject.toJSONString(result));
        }else{
            result = Result.error(i);
            log.info("方法名称：用户添加订单，出参：{}", JSONObject.toJSONString(result));
        }
        return result;
    }

    //司机接单
    @PostMapping("/driverGetOrder")
    public Result driverGetOrder(@RequestBody DriverRequest driverRequest){
       return  Result.success(orderService.driverGetOrder(driverRequest));
    }

    @PostMapping("/syncFenceDriver")
    public Result<Map<String, LinkedList<String[]>>> syncFenceDriver(){
//        return Result.success(orderService.syncFenceDriver());
        return Result.success();
    }

    // TODO 填入司机订单信息 -用户调用接口
    @PostMapping("/updateOrderByDriver")
    public Result updateOrderByDriver(@RequestBody OrderRequest orderRequest) {
        log.info("方法名称：填入司机订单信息，入参:{}", JSON.toJSONString(orderRequest));
        int i = orderService.updateOrderByDriver(orderRequest);
        Result result = null;
        if (i > 0) {
            result = Result.success(i);
            log.info("方法名称：填入司机订单信息，出参：{}", JSONObject.toJSONString(result));
        } else {
             result  = Result.error(i);
            log.info("方法名称：填入司机订单信息，出参：{}", JSONObject.toJSONString(result));
        }
        return result;
    }


    // TODO 通过接单状态查询订单
    @PostMapping("queryOrderByOrderState")
    public Result<List<OrderRequest>> queryOrderByOrderState(@RequestBody OrderStatePageVo orderStatePageVo) {
//        log.info("方法名称：通过接单状态查询订单，入参：{}", JSONObject.toJSONString(orderStatePageVo));
//        List<OrderRequest> orders = orderService.queryOrderByOrderState(orderStatePageVo);
//        log.info(orders.toString());
//        Result<List<OrderRequest>> result = Result.success(orders);
//        log.info("方法名称：通过接单状态查询订单，出参：{}", JSONObject.toJSONString(result.getMsg()));
        return Result.success();
    }

    //司机查询订单
    @PostMapping("/getOrder")
    public SendOrderResponse sendOrder(@RequestBody FenceRequest fenceRequest){
        return orderService.sendOrder(fenceRequest);
    }

    @PostMapping("/selectOrder")
    public Result<List<OrderRequest>> selectOrder(@RequestBody OrderRequest orderRequest){
        List<OrderRequest> result = orderService.selectOrder(orderRequest);
        return Result.success(result);
    }

    /**
     * 根据手机号查用户订单
     */
    @PostMapping("/orderFindByPhone")
    public Result<List<OrderRequest>> orderFindByPhone(@RequestParam String personPhone){
       List<OrderRequest> OrderFindByPhone= orderService.orderFindByPhone(personPhone);
       return Result.success(OrderFindByPhone);
    }

    @PostMapping("/selectPcOrder")
    public Result<List<OrderRequest>> selectPcOrder(){
        List<OrderRequest> result = orderService.selectPcOrder();
        return Result.success(result);
    }


    /**
     * 修改订单
     * @param orderRequest
     * @return
     */
    @PostMapping("/updatePc")
    public Result<Integer> updatePc(@RequestBody OrderRequest orderRequest){
        int i = orderService.updatePc(orderRequest);
        return Result.success(i);
    }


    @PostMapping("/echoOrder")
    public Result<OrderRequest> findByIdOrder(@PathVariable("id") Long id){
        OrderRequest orderRequest = orderService.echoOrder(id);
        return Result.success(orderRequest);
    }




}


